package com.example.clublesson01

import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import android.app.Dialog
import android.view.LayoutInflater
import android.view.WindowManager
import android.widget.TextView
import android.text.SpannableString
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.content.Intent
import com.example.clublesson01.R
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.kf.lib_base.base.BaseViewActivity
import com.example.clublesson01.databinding.ActivityWelcomeBinding
@Route(path = "/app/WelcomeActivity")
class WelcomeActivity : BaseViewActivity<ActivityWelcomeBinding>() {


    override fun iniView() {
        // 进入页面自动弹窗
      //  showWelcomeDialog()
        val hasAgreed = SPUtils.getInstance().getBoolean("isFirst", false)
        if (!hasAgreed) {
            showWelcomeDialog()
        } else {
            // 已经同意，直接跳转到 WelBannerActivity
            ARouter.getInstance().build("/app/banner/WelBannerActivity").navigation()
            finish() // 可选：避免用户按返回键回到 WelcomeActivity
        }

    }

    private fun showWelcomeDialog() {
        val dialog = Dialog(this)
        val view = LayoutInflater.from(this).inflate(R.layout.dialog_privacy, null)
        dialog.setContentView(view)
        dialog.setCancelable(false)
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
        dialog.window?.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT)

        val tvCancel = view.findViewById<TextView>(R.id.tvCancel)
        val tvConfirm = view.findViewById<TextView>(R.id.tvConfirm)
        val tvContent = view.findViewById<TextView>(R.id.tvContent)

        // 富文本设置
        val content = "您使用“俱乐部”服务之前，请仔细阅读《用户协议》和《隐私政策》，我们将严格按照您同意的各项条款使用您的个人信息，以便为您提供更好的服务。如您同意此政策，请点击“同意并继续”开始使用我们的产品和服务，我们将竭力保证您的个人信息安全"
        val userAgreement = "《用户协议》"
        val privacyPolicy = "《隐私政策》"
        val start1 = content.indexOf(userAgreement)
        val end1 = start1 + userAgreement.length
        val start2 = content.indexOf(privacyPolicy)
        val end2 = start2 + privacyPolicy.length

        val spannable = SpannableString(content)
        // 用户协议点击和红色
        spannable.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
//                val intent = Intent(this@WelcomeActivity, com.example.clublesson01.web.WebActivity::class.java)
//                intent.putExtra("url", "user_agreement_url")
//                intent.putExtra("title", "用户协议")
//                startActivity(intent)
                ARouter.getInstance().build("/app/web/WebActivity")
                    .withString("url","https://www.bilibili.com")
                    .navigation()
            }
        }, start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannable.setSpan(ForegroundColorSpan(0xFFFF0000.toInt()), start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        // 隐私政策点击和红色
        spannable.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
//                val intent = Intent(this@WelcomeActivity, com.example.clublesson01.web.WebActivity::class.java)
//                intent.putExtra("url", "privacy_policy_url")
//                intent.putExtra("title", "隐私政策")
//                startActivity(intent)
                ARouter.getInstance().build("/app/web/WebActivity")
                    .withString("url","https://www.bilibili.com")
                    .navigation()
            }
        }, start2, end2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannable.setSpan(ForegroundColorSpan(0xFFFF0000.toInt()), start2, end2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        tvContent.text = spannable
        tvContent.movementMethod = LinkMovementMethod.getInstance()

        tvCancel.setOnClickListener {
            dialog.dismiss()
            finishAffinity() // 退出应用
        }
        tvConfirm.setOnClickListener {
            dialog.dismiss()
            SPUtils.getInstance().put("isFirst",true)
            ARouter.getInstance().build("/app/banner/WelBannerActivity").navigation()

        }
        dialog.show()
    }

    override fun iniData() {

    }

    override fun Layoutid(): Int {
         return R.layout.activity_welcome
    }
}